<script setup lang="ts">
import {useRoute} from "vue-router";
import {onMounted, ref} from "vue";
import {addLocationAPI, getOneLocationAPI, updateLocationAPI} from "@/apis/location.js";
import {useGetUserById, useGetUserList} from "@/apis/user.js";
import {getOneProjectAPI, getProjectListAPI} from "@/apis/projectInfo.js";
import {useGetAllWarehouseAPI, useGetWarehouseByIdAPI} from "@/apis/warehouse.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const route = useRoute();

interface locationType{
  id:number,
  userId:number,
  userName:string,
  realName:string,
  projectId:number,
  projectName:string,
  warehouseId:number,
  warehouseName:string,
  locationAddress:string,
  description:string,
}
const locationInfo=ref<locationType>({
  id:0,
  userId:0,
  userName:'',
  realName:'',
  projectId:0,
  projectName:'',
  warehouseId:0,
  warehouseName:'',
  locationAddress:'',
  description:'',
});
interface userType{
  id:number,
  loginName:string,
  realName:string,
}
const userInfo=ref<userType>({
  id:0,
  loginName:'',
  realName:'',
})
const userList=ref<userType[]>();
interface projectType{
  id:number,
  name:string,
  address:string,
}
const projectList=ref<projectType[]>();
const projectInfo=ref<projectType>({
  id:0,
  name:'',
  address:'',

})
interface warehouseType{
 id:number,
 name:string,
 address:string,
}
const warehouseList=ref<warehouseType[]>();
const warehouseInfo=ref<warehouseType>({
  id:0,
  name:'',
  address:'',
})

const locationAddressList=ref<any[]>([]);

const loginNameFlag=ref(true)
const realNameFlag=ref(true)




onMounted(async () => {
  if(route.params.id===null||route.params.id===undefined){
   loginNameFlag.value=false;
   realNameFlag.value=true;
  }
  else {
   await getOneLocationAPI(Number(route.params.id)).then((res:any) => {
      locationInfo.value=res.data;
   })
  }

 await useGetUserList(1,1000).then((res:any)=>{
  userList.value=res.data;
 })

  await  useGetUserById( locationInfo.value.userId).then((res:any)=>{
   userInfo.value=res.data;
  })

  await getProjectListAPI(1,100).then((res:any)=>{
    projectList.value=res.data;
    projectList.value.push({});
  })

  await getOneProjectAPI(locationInfo.value.projectId).then((res:any)=>{
    projectInfo.value=res.data;
  })

  await useGetAllWarehouseAPI(1,100).then((res:any)=>{
    warehouseList.value=res.data;
    warehouseList.value.push({});
  })

  await useGetWarehouseByIdAPI(locationInfo.value.warehouseId).then((res:any)=>{
    warehouseInfo.value=res.data;
  })



})

const updateLocationSummit=async ()=> {
  if (route.params.id === null || route.params.id === undefined) {
    locationInfo.value.id=null;
    locationInfo.value.userId=userInfo.value.id;
    locationInfo.value.userName=userInfo.value.loginName;
    locationInfo.value.realName=userInfo.value.realName;

    await  addLocationAPI(locationInfo.value).then((res:any)=>{
    if(res.code===200){
      ElMessage({
        type:'success',
        message:'添加成功',
      })
      router.push("/manage/locationManager");
    }
    else {
      ElMessage({
        type:'error',
        message:res.message,
      })
      // window.location.reload();
    }
    return;
  })
  }


  else {
  await  updateLocationAPI(locationInfo.value).then((res: any) => {
    if(res.code===200){
      ElMessage({
        type:'success',
        message:'修改成功',
      })
      router.push("/manage/locationManager");
    }
    else {
      ElMessage({
        type:'error',
        message:res.message,
      })
      // window.location.reload();
    }
    return;
    })
  }
}
const projectSelectChange=(val:any)=>{
  if(val.id===projectInfo.value.id){
    locationAddressList.value.push(projectInfo.value.address)
  }

  console.log(locationAddressList.value);

  locationInfo.value.projectName=val.name;
  locationInfo.value.projectId=val.id;
  locationInfo.value.locationAddress=val.address;
}

const warehouseSelectChange=(val:any)=>{


  if(val.id===warehouseInfo.value.id){
    locationAddressList.value.push(warehouseInfo.value.address)
    locationAddressList.value.push('');
  }

  console.log(locationAddressList.value);

  locationInfo.value.warehouseName=val.name;
  locationInfo.value.warehouseId=val.id;
}




</script>

<template>

  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/manage/locationManager`}" >Location列表</el-breadcrumb-item>
      <el-breadcrumb-item  ><span v-show="loginNameFlag">修改location</span><span v-show="loginNameFlag===false">新增location</span></el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <div class="divTable">
    <table class="table">
      <tbody>
      <tr>
        <td class="tableTitle" colspan="4" ><h3 v-show="loginNameFlag">修改location</h3><h3 v-show="loginNameFlag===false">新增location</h3></td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF;color: #242424;font-family: 微软雅黑,serif">登录名</td>
<!--        <td style="width: 350px;height: 15px;   text-align: center  "><el-input  v-model="locationInfo.userName" /></td>-->
        <el-select
            value-key="id"
            v-model="userInfo"
            placeholder="Select"
            size="large"
        >
          <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.loginName"
              :value="item"
              :disabled="loginNameFlag"
          />
        </el-select>


        <td style="width: 250px;height: 15px; text-align: center ;background: #EFF6FF"  >真实姓名</td>
<!--        <td style="width: 350px;height: 15px ; text-align: center   "><el-input  v-model="locationInfo.realName" /></td>-->

        <el-select
            value-key="id"
            v-model="userInfo"
            placeholder="Select"
            size="large"
        >
          <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.realName"
              :value="item"
              :disabled="realNameFlag"
          />
        </el-select>


      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">项目名称</td>
<!--        <td style="width: 350px;height: 15px;text-align: center  "><el-input  v-model="locationInfo.projectName" /></td>-->
        <el-select
            value-key="id"
            v-model="projectInfo"
            placeholder="Select"
            size="large"
            @change="projectSelectChange"
        >
          <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.name"
              :value="item"
          />
        </el-select>



        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >仓库名称</td>
<!--        <td style="width: 350px;height: 15px;text-align: center   "><el-input  v-model="locationInfo.warehouseName" /></td>-->
        <el-select
            value-key="id"
            v-model="warehouseInfo"
            placeholder="Select"
            size="large"
            @change="warehouseSelectChange"
        >
          <el-option
              v-for="item in warehouseList"
              :key="item.id"
              :label="item.name"
              :value="item"
          />
        </el-select>
      </tr>
      <tr>

<!--        locationAddress，有问题，要考虑到底用Project的地址还是warehouse的地址-->
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">Location地址</td>
        <td style="width: 350px;height: 15px;text-align: center  ">
<!--          <el-input  v-model="locationInfo.locationAddress" />-->
          <el-select
              value-key="id"
              v-model="locationInfo.locationAddress"
              placeholder="Select"
              size="large"
          >
            <el-option
                v-for="item in locationAddressList"
                :label="item.address"
                :value="item"
            />
          </el-select>


        </td>
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >备注</td>
        <td style="width: 350px;height: 15px;text-align: center   "><el-input  v-model="locationInfo.description" /></td>
      </tr>
      <tr>
        <td colspan="4" style="background:#EFF6FF ">
          <el-button @click="updateLocationSummit" type="primary"  style="margin-top: 1%;margin-bottom: 1%" >确认</el-button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>



</template>

<style scoped lang="scss">

.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}


.divTable {
  position: static;
  top: 150px;
  bottom: 0;
  left: 300px;
  right: 0;
  margin: auto;
}


table, td,tr{
  border: 1px solid #44a8da;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
  height: 45px;
}

.tableTitle{

  border-right: 0 none;
  border-left: 0 none;
  text-align: center;
  font-size: large;
  font-weight: bold;
}

.demo-image__error .image-slot {
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}
.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}



</style>